package hibernate.xml43.view;

import hibernate.xml43.pojo.Dept;
import hibernate.xml43.util.HibernateHandler;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


public class View4 {
public static void main(String[] args) {
	Session session = HibernateHandler.getCurrentSession();
	Transaction transaction = null;
	try{
		transaction = session.beginTransaction();
		//↓↓↓↓↓↓↓HQL↓↓↓↓↓↓↓
		//from后面是对象的名称(而不是表名)
		//where后面是对象的属性(直接用表的字段名可以但是不推荐)
		Query query = session.createQuery("from Dept where id=1");
		@SuppressWarnings("unchecked")
		List<Dept> deptList = query.list();
		String dname = deptList.get(0).getDname();
		System.out.println(dname);
		
		transaction.commit();
		
	}catch(Exception e){
		if(transaction!=null){
			transaction.rollback();
		}
	}finally{
		if(session!=null){
			session.close();
		}
	}
	
}
}
